#include "system_S32K144.h"
#include "Cpu.h"
#include "user_define.h"
#include "timers.h"
#include <string.h>
#include "bsp_uart_cfg.h"
#include "bsp_uart_driver.h"
#include "Tps929120_PBCfg.h"
#include "bsp_board_driver.h"

uint16_t receiveByteNum = 0;
uint8_t g_u8rcvBuffer[16];
uint8_t g_u8Diag_Databuff[16];
uint8_t g_u8Diag_Databuff_Right[16];

void rxCallback(void *driverState, uart_event_t event, void *userData)
{
    /* Unused parameters */
    (void)driverState;
    (void)userData;

    /* Check the event type */
    if (event == UART_EVENT_RX_FULL)
    {
		receiveByteNum++;
		LPUART_DRV_SetRxBuffer(INST_LPUART1, &g_u8rcvBuffer[receiveByteNum], 1U);
    }
}

void Bsp_UART_Init(void)
{
    LPUART_DRV_Init(INST_LPUART1, &lpuart1_State, &lpuart1_InitConfig0);

	INT_SYS_EnableIRQ(LPUART0_RxTx_IRQn);
    INT_SYS_SetPriority(LPUART0_RxTx_IRQn, 0x01);

	LPUART_DRV_InstallRxCallback(INST_LPUART1, rxCallback, NULL);
}

void uartWrite(uint8_t commandFrame[], uint8_t frameLength, uint8_t checkResponse, uint8_t responseLength)
{
#if 1
	uint8_t i =0;

	if(checkResponse != 0)
	{
		LPUART_DRV_ReceiveData(INST_LPUART1, g_u8rcvBuffer, 1);
	}
	else
	{
		LPUART_DRV_AbortReceivingData(INST_LPUART1);
	}
    

	LPUART_DRV_SendDataBlocking(INST_LPUART1, commandFrame, frameLength, 2);

	if(checkResponse == TRUE)
	{
		 /* launch timer and set wait time = 2000us
	      * This time should be larger than the time to receive all the response bytes,
	      * And the response receiving time depends on the buard rate and the number of response byte,
	      * For example, baurd rate = 500000, 2 response byte, so the wait time should be larger than 2*10*1/500000 = 40us
	      * Why 2*10 because for each byte there are additional 1 start bit and 1 stop bit
	      */
		timeOut(2000);
		/* received all response byte or the wait time exceeds the specified time */
		while(!((timeOutFlag == 1) || (receiveByteNum == responseLength)));

		/*Take some action when successfully received response*/
		if(receiveByteNum == responseLength)
		{
			/* Toggle Green LED */
			LPUART_DRV_AbortReceivingData(INST_LPUART1);

			if(g_u8rcvBuffer[1] == TPS_CHIP_ADD_14)
			{
				memcpy(g_u8Diag_Databuff, g_u8rcvBuffer, responseLength);
			}

			if(g_u8rcvBuffer[1] == TPS_CHIP_ADD_15)
			{
				memcpy(g_u8Diag_Databuff_Right, g_u8rcvBuffer, responseLength);
			}

		}
		else
		{
			if((responseLength - receiveByteNum) >= 2)
			{
				if(g_u8rcvBuffer[1] == TPS_CHIP_ADD_14)
				{
					g_u8Diag_Databuff[7] = 0xFF;
					memcpy(g_u8Diag_Databuff, g_u8rcvBuffer, receiveByteNum);
				}
				if(g_u8rcvBuffer[1] == TPS_CHIP_ADD_15)
				{
					g_u8Diag_Databuff_Right[7] = 0xFF;
					memcpy(g_u8Diag_Databuff_Right, g_u8rcvBuffer, receiveByteNum);
				}
			}
		}

		/* Clear data sent */
		for(i=0; i<frameLength; i++)
		{
			// commandFrame[i] = 0x00;
		}
		/* Clear response data*/
		for(i=0; i<responseLength; i++)
		{
			g_u8rcvBuffer[i] = 0x00;
		}
		/* reset number of response data*/
		receiveByteNum = 0;

		/* Disable the receiver and receive data full interrupt of LPUART1 */
		LPUART_DRV_AbortReceivingData(INST_LPUART1);

	}
#endif
}